/**
 * Created by IntelliJ IDEA.
 * User: user
 * Date: 10.10.12
 * Time: 19:01
 * To change this template use File | Settings | File Templates.
 */
package zakladki.AS3.country {
import mx.collections.XMLListCollection;
import mx.controls.List;
import mx.controls.TextInput;

public class CountryFilterListener {

    private var txtSearch:TextInput;
    private var countryXMLList:XMLListCollection;
    private var countryList:List;


    public function CountryFilterListener(txtSearch:TextInput, countryXMLList:XMLListCollection,countryList:List) {
        this.txtSearch = txtSearch;
        this.countryXMLList = countryXMLList;
        this.countryList = countryList;
    }

    public function textSearchedChanged():void {
        var txt:String = new String("");
        txt = txtSearch.text;

        if (txtSearch.text == "") {
            countryXMLList.filterFunction = null;
        }
        else if (txtSearch.text != "") {
            countryXMLList.filterFunction = countryFilterFunction;
        }
        countryXMLList.refresh();
        if(countryXMLList.length > 0){
           countryList.selectedIndex = 0;
           countryList.scrollToIndex(0);
        }
    }

    public function countryFilterFunction(item:Object):Boolean {
        var result:Boolean = new Boolean(false);

        if (item.toString().toLowerCase().search(txtSearch.text.toLowerCase()) != -1) {
            result = true;
        }
        else {
            result = false;
        }
        return result;
    }
}
}
